<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <link rel="stylesheet" href="/layui/css/layui.css"  media="all">
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">

    <div class="layui-container" style="width: 500px;height: 330px;padding-top: 60px;">
        <form class="layui-form"  action="" method="post"  name="user">
            <div class="layui-form-item">
                <label class="layui-form-label">ID：</label>
                <div class="layui-inline">
                    <input type="text" id="id" name="id" lay-verify="id" autocomplete="off" placeholder="请输入商品ID" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称：</label>
                <div class="layui-inline">
                    <input type="password" id="name" name="name" lay-verify="password" placeholder="请输商品名称" autocomplete="off" class="layui-input" value="商品测试">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">价格：</label>
                <div class="layui-inline">
                    <input type="text" id="price"  name="price" lay-verify="required" placeholder="请输入价格" autocomplete="off" class="layui-input" value="123">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="state" value="0" title="上架" checked="">
                    <input type="radio" name="state" value="1" title="暂不上架">
<!--                    <input type="radio" name="sex" value="禁" title="禁用" disabled="">-->
                </div>
            </div>

            <div class="layui-upload">
                <label class="layui-form-label">上传图片</label>
                <div class="layui-upload-list">
                    <input type="file"  accept="image/jpg,image/png,image/jpeg"
                           name="image" id="image" >
                </div>
            </div>

            <div class="layui-form-item">
                <input type="button" class="layui-btn" lay-submit="" id="commit" style="margin-left: 160px;" value="提交"></input>
            </div>

        </form>
    </div>
</div>
<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/layui/layui.all.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'element','upload'], function(){
        var form = layui.form;
        var element = layui.element;
        var $ = layui.$;
        var upload = layui.upload;


            // ajax上传


});
</script>
<script type="text/javascript" th:src="@{/js/jquery-3.4.1.js}"></script>
<script>
    $(function(){

        $("#commit").click(function () {
            console.log("enter");
            var id = $("#id").val();
            var name = $("#name").val();
            var price = $("#price").val();
            var state = $('input[type=radio][name=state]:checked').val();
            // 获取图片
            var logoUrl = document.getElementById("image");
            var file = logoUrl.files[0];

            console.log(id);
            console.log(state);

            var formdata=new FormData();
            formdata.append("image",file);
            formdata.append("id",id);
            formdata.append("name",name);
            formdata.append("price",price);
            formdata.append("state",state);
            var token = layui.data('token').data;
            $.ajax({
                url:"/menu/save",//这里是请求的地址
                type: 'POST',
                data: formdata,
                dataType: "json",
                headers:{"token":token},
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success:function(result){
                    // window.location.href = "admin.html"
                }
                ,error:function(result){

                }
            });
        });
    });

</script>
</body>
</html>